#ifndef OGREREFAPPUTILS_H
#define OGREREFAPPUTILS_H
//------------------------------------------------------------------------
//
//  Name:   State.h
//
//  Desc:   abstract base class to define an interface for a state
//
//  Author: Mat Buckland (fup@ai-junkie.com)
//
//------------------------------------------------------------------------
#include <math.h>
#include <limits>


namespace OgreRefApp
{


    inline Real RandFloat()
    {
        return ((rand())/(RAND_MAX+1.0));
    }

    inline double RandInRange(Real x, Real y)
    {
        return x + RandFloat()*(y-x);
    }

//returns a random double in the range -1 < n < 1
    inline double RandomClamped()
    {
        return RandFloat() - RandFloat();
    }

    //------------------------------------------------------------------------
//  Given a point P and a circle of radius R centered at C this function
//  determines the two points on the circle that intersect with the
//  tangents from P to the circle. Returns false if P is within the circle.
//
//  thanks to Dave Eberly for this one.
//------------------------------------------------------------------------
    inline bool GetTangentPoints (Ogre::Vector3 C, Real R, Ogre::Vector3 P, Ogre::Vector3& T1, Ogre::Vector3& T2)
    {
        Ogre::Vector3 PmC = P - C;
        Real SqrLen = PmC.squaredLength();
        Real RSqr = R*R;
        if ( SqrLen <= RSqr )
        {
            // P is inside or on the circle
            return false;
        }

        Real InvSqrLen = 1/SqrLen;
        Real Root = sqrt(fabs(SqrLen - RSqr));

        T1.x = C.x + R*(R*PmC.x - PmC.y*Root)*InvSqrLen;
        T1.y = C.y + R*(R*PmC.y + PmC.x*Root)*InvSqrLen;
        T2.x = C.x + R*(R*PmC.x + PmC.y*Root)*InvSqrLen;
        T2.y = C.y + R*(R*PmC.y - PmC.x*Root)*InvSqrLen;

        return true;
    }

    //returns a random integer between x and y
    inline int   RandInt(int x,int y)
    {
      assert(y>=x && "<RandInt>: y is less than x");
      return rand()%(y-x+1)+x;
    }


}
#endif
